class Admin::CategoriesController < ApplicationController
	before_filter :login_required
	before_filter :find_category, :only=>[:edit, :update, :destroy]
	cache_sweeper :category_sweeper, :only=>[:create, :update, :destroy]
	
	def index
		@categories = Category.root.paginate(:page=>params[:page], :per_page=>20, :order=>"position")
	end
	
	def new
		@category = Category.new
	end
	
	def create
		@category = Category.new(params[:category])
		@category.save!
		redirect_to admin_categories_path
		rescue ActiveRecord::RecordInvalid
    	render :action => 'new'
	end
	
	def edit
	end
	
	def update
		@category.update_attributes!(params[:category])
		redirect_to admin_categories_path
		rescue ActiveRecord::RecordInvalid
    	render :action => 'edit'
	end
	
	def destroy
		@category.destroy
		redirect_to admin_categories_path
	end
	
	private
	
	def find_category
		@category = Category.find_by_slug(params[:id])
	end
end
